//
//  JPPayView.m
//  DHJPHotel
//
//  Created by Apple on 2018/5/21.
//  Copyright © 2018年 Apple. All rights reserved.
//

#import "JPPayView.h"
#import "JPPayHeaderView.h"
#import "JPPayCell.h"
#import "JPPayFootView.h"
NSString *payCellid = @"payCellid";
@interface JPPayView()<UITableViewDataSource,JPPayCellDelegate>
@property(nonatomic,strong)UITableView *payView;
@property(nonatomic,strong)UIView *backgroundView;
@property(nonatomic,strong)JPPayHeaderView *header;
@property(nonatomic,strong)UIButton *currentBtn;

@end
@implementation JPPayView

- (void)setDictM:(NSDictionary *)dictM{
    _dictM = dictM;
    self.header.dict = dictM;
}
- (void)setPayMemetList:(NSMutableArray *)payMemetList
{
    JPPayMentModel *payment = [[JPPayMentModel alloc] init];
    payment.payment_name = @"余额支付";
    _payMemetList = payMemetList;
    
    [_payMemetList insertObject:payment atIndex:0];
    
    [self.payView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = [UIColor blackColor];
        [self addSubview:self.backgroundView];
        self.backgroundView.alpha = 0.4;
       // self.alpha = 0.4;
        self.payView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height * 0.55, kScreenWidth, self.bounds.size.height * 0.45) style:UITableViewStylePlain];
       self.payView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.payView];
        self.payView.dataSource = self;
        self.header = [JPPayHeaderView headerView];
        JPPayFootView *footer = [JPPayFootView footView];
        self.payView.tableHeaderView = self.header;
        self.payView.tableFooterView = footer;
        
        footer.confirmPayButtonClick =
        ^{
            if (!self.currentBtn) {
                [SVProgressHUD showErrorWithStatus:@"请选择支付方式"];
                return ;
            }
            switch (self.currentBtn.tag) {
                case 0:
                    // 余额支付
                    NSLog(@"....");
                    [self requestBalancePay];
                    break;
                case 1:
                    // 支付宝
                    //FIXME: - 未实现
                    [self doAlipay];
                    break;
                default:
                    // 微信
                    //FIXME: - 未实现
                    [self doWecheatPay];
                    break;
            }
        };
        [self.payView registerNib:[UINib nibWithNibName:@"JPPayCell" bundle:nil] forCellReuseIdentifier:payCellid];
        self.payView.sectionHeaderHeight = self.payView.bounds.size.height * 0.3;
        self.payView.rowHeight = self.payView.bounds.size.height * 0.5 / 3;
        self.payView.sectionFooterHeight = self.payView.bounds.size.height * 0.2;
        
        self.payView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}
#pragma mark: - 余额支付请求
- (void)requestBalancePay
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"key"] = [DHNetworkManager.shareManager userAccount].key;
    param[@"pay_id"] = self.dictM[@"pay_id"];
    param[@"price"] = self.dictM[@"price"];
    [self.delegate payView:self balancePay:param];


}

#pragma mark: - 支付宝支付
-(void)doAlipay
{
    [self.delegate payView:self aliPay:nil];
}

- (void)doWecheatPay
{
    [self.delegate payView:self wechaeat:nil];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [UIView animateWithDuration:0.25 animations:^{
        self.transform = CGAffineTransformIdentity;
    }];
}
#pragma mark: - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.payMemetList.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JPPayCell *cell = [tableView dequeueReusableCellWithIdentifier:payCellid forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.payMemetList[indexPath.row];
    cell.delegate = self;
    cell.tag = indexPath.row;
    return cell;
}

- (void)payCell:(JPPayCell *)cell selectedPayMothed:(UIButton *)payButton
{
    self.currentBtn.selected = false;
    payButton.selected = YES;
    self.currentBtn = payButton;
}
@end
